<template>
    <base-breadcrumb :title="$route.meta.title">
        <!-- 职工代表大会编辑 -->
        <a-form-model
            ref="ruleForm"
            :model="form"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            :rules="editModel != 0 ? rules : {}"
        >
            <a-spin :spinning="loading">
                <!-- 表格 -->
                <a-card :bordered="false">
                    <title-name title="基本信息"></title-name>
                    <custom-form
                        :formList="formList"
                        :form="form"
                        :editModel="editModel == 0 ? '' : ''"
                        @change="change"
                        @selectOrg="selectOrg"
                    >
                        <!-- 附件上传 -->
                        <template #fileList>
                            <template v-if="editModel != 0">
                                <l-table businessCode="BM27" :uploadArrys.sync="form.fileList"></l-table>
                                <!-- <div class="file_prompt">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</div> -->
                            </template>
                            <file-link v-else :fileList="form.fileList" style="margin-top: -3px"></file-link>
                        </template>
                    </custom-form>
                </a-card>
            </a-spin>
            <!-- 页脚操作栏 -->
            <div class="btn_box page-btn-right-top" v-if="editModel != 0">
                <!-- <a-button class="button_right" v-if="id != '' && form.auditStatus == 'DRAFT'" type="danger" @click="handleRemove()"
                    >删除草稿</a-button
                > -->
                <a-button class="button_right" type="primary" :loading="loading.DRAFT" @click="handleSubmit('DRAFT')"
                    >保存草稿</a-button
                >
                <a-button
                    class="button_right"
                    type="primary"
                    :loading="loading.SUBMITTED"
                    @click="handleSubmit('SUBMITTED')"
                    >提交</a-button
                >
                <a-button type="primary" @click="handleActiveClose">关闭</a-button>
            </div>
            <div class="btn_box page-btn-right-top" v-else>
                <a-button type="primary" @click="handleActiveClose">关闭</a-button>
            </div>
        </a-form-model>
         
    </base-breadcrumb>
</template>

<script>
import { addContract, getContract } from '@/api/businessAffairs/revenueAndCost'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import SelectPersonModal from '@/components/SelectPersonModal'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
export default {
    name: 'a' + Date.now(),
    extends: new BaseEditPage(),
    components: {
        SelectPersonModal
    },
    data() {
        return {
            loading: false, // 加载控件
            visible: false, //人员选择控件
            editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
            form: { creatorName: '', fillDate: '',projectId:null}, // 表单
            id: '',
            loadingBtn: {
                DELETE: false,
                DRAFT: false,
                SUBMITTED: false,
                REJECTED: false
            },
            rules: {
                dptNameForA: [{ required: true, message: '请输入' }],
                contractNameForA: [{ required: true, message: '请输入' }],
                contractFeeForA: [{ required: true, message: '请输入' }],
                totalContractManagementFee: [{ required: true, message: '请输入' }],

                totalOutContractManagementFee: [{ required: true, message: '请输入' }],
                conditionsDescribed: [{ required: true, message: '请输入' }],
                wayOfAccess: [{ required: true, message: '请选择' }],

                fileList: [{ type: 'array', required: true, message: '请上传附件', trigger: 'blur' }]
            }, // 校验规则
            init: false, // 初始化控件
            removeApi: { remove: {} } // api
        }
    },
    watch: {
        // 监听附件实现附件校验
        'form.fileList'() {
            if (this.editModel != 0 && this.init) {
                this.$nextTick(() => {
                    this.$refs.ruleForm?.validateField('fileList')
                })
            }
        }
    },
    mounted() {
        this.$route.meta.title = ['总承包管理费查看', '总承包管理费编辑', '总承包管理费新增'][this.editModel]
        // 不为添加时请求详情
        if (this.editModel != 2) {
            this.loading = true
            getContract(this.id)
                .then((res) => {
                    this.loading = false
                    console.log(res)
                    this.form = res[1].data
                    console.log(this.form)
                    this.form.overrideTotalManagementFee = this.form.overrideTotalManagementFee ? '是' : '否'
                    this.$nextTick(() => {
                        this.init = true
                    })
                })
                .catch(() => {
                    this.loading = false
                })
        } else {
            this.form.creatorName = JSON.parse(sessionStorage.getItem('ycloud-user_info'))?.name // 默认填报人
            this.form.fillDate = this.$dateFormat(new Date(), 'D') // 默认填报时间
            this.form.projectId = this.$userInfo().currentPost.projectId;
            this.$nextTick(() => {
                this.init = true
            })
        }
    },
    computed: {
        formList() {
            return [
                // {
                //     type: SelectSubCompany,
                //
                //     keyName: 'companyId',
                //     label: '分公司名称',
                //     disabled: true,
                //     attrs: {
                //         isDefaultSelected: this.editModel == 2,
                //         autoSelected: this.editModel == 2
                //     }
                // },
                // {
                //     type: SelectProject,
                //
                //     keyName: 'projectId',
                //     label: '项目名称',
                //     disabled: true,
                //     attrs: {
                //         isDefaultSelected: this.editModel == 2,
                //         autoSelected: this.editModel == 2,
                //         keyValue: 'companyId',
                //         isClear: this.editModel == 2,
                //         isEdit: this.editModel != 2,
                //         form: this.form
                //     }
                // },
              {
                label: '所属单位',
                type: 'orgTree',
                keyName: 'projectId',
                rules: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
                span: 16,
                isAutoSelectFirst: this.editModel == 2,
                isQueryProjecDetail: false,
                isNeedAuth:true ,
                checkStrictly: true,
                disabled: true,
              },
                {
                    keyName: 'dptNameForA',
                    type: 'BaseInput',
                    disabled: this.editModel == 0,
                    label: '甲指单位名称'
                },
                {
                    keyName: 'contractNameForA',
                    type: 'BaseInput',
                    disabled: this.editModel == 0,
                    label: '甲指单位合同名称'
                },
                {
                    keyName: 'contractFeeForA',
                    type: 'BaseInputNumber',
                    disabled: this.editModel == 0,
                    precision: 2,
                    label: '甲指单位合同额（含税）（元）'
                },
                {
                    keyName: 'totalContractManagementFee',
                    type: 'BaseInputNumber',
                    disabled: this.editModel == 0,
                    precision: 2,
                    label: '总包合同内应收管理费（含税)（元）'
                },
                {
                    keyName: 'contractManagementRate',
                    type: 'BaseInputNumber',
                    disabled: true,
                    precision: 2,
                    label: '合同内管理费收取率%'
                },
                {
                    keyName: 'totalOutContractManagementFee',
                    type: 'BaseInputNumber',
                    disabled: this.editModel == 0,
                    precision: 2,
                    label: '总包合同外额外管理费收取金额（含税）（元）'
                },
                {
                    keyName: 'outContractManagementRate',
                    type: 'BaseInputNumber',
                    disabled: true,
                    precision: 2,
                    label: '合同外额外收取管理费率（%）'
                },
                {
                    keyName: 'accumulativeManagementFee',
                    type: 'BaseInputNumber',
                    disabled: true,
                    precision: 2,
                    label: '累计管理费收取金额（含税）（元）'
                },
                {
                    keyName: 'accumulativeTotalManagementRate',
                    type: 'BaseInputNumber',
                    disabled: true,
                    precision: 2,
                    label: '累计总包管理费收取费率（%）'
                },
                {
                    keyName: 'overrideTotalManagementFee',
                    type: 'BaseInput',
                    disabled: true,
                    label: '是否突破总包合同收取管理费'
                },
                {
                    keyName: 'conditionsDescribed',
                    type: 'BaseTextarea',
                    label: '总包合同相关条款描述',
                    disabled: this.editModel == 0,
                    span: 24
                },
                {
                    type: 'a-select',
                    keyName: 'wayOfAccess',
                    label: '收取形式',
                    span: 8,
                    disabled: this.editModel == 0,
                    data: [
                        {
                            label: '水电费',
                            value: 1
                        },
                        {
                            label: '垃圾清运费',
                            value: 2
                        },
                        {
                            label: '管理费',
                            value: 3
                        },
                        {
                            label: '其他',
                            value: 4
                        }
                    ]
                },
                {
                    keyName: 'creatorName',
                    type: 'a-input',
                    disabled: true,
                    label: '填报人'
                },
                {
                    keyName: 'fillDate',
                    type: 'a-date-picker',
                    disabled: true,
                    label: '填报时间',
                    attrs: {
                        valueFormat: 'YYYY-MM-DD'
                    }
                },
                {
                    slot: true,
                    keyName: 'fileList',
                    label: '*附件上传',
                    span: 24
                }
            ]
        }
    },
    methods: {
      selectOrg(val) {
        this.form.companyId = val[val.length - 2].orgId
      },
        /**
         * 输入框事件
         */
        change(key) {
            //合同内管理费收取率
            if (key == 'totalContractManagementFee' || key == 'contractFeeForA') {
                if (
                    (this.form.totalContractManagementFee || this.form.totalContractManagementFee == 0) &&
                    (this.form.contractFeeForA || this.form.contractFeeForA == 0)
                ) {
                    this.form.contractManagementRate =
                        (this.form.totalContractManagementFee / this.form.contractFeeForA) * 100 || undefined
                }
            }
            //合同外额外收取管理费率
            if (key == 'totalOutContractManagementFee' || key == 'contractFeeForA') {
                if (
                    (this.form.totalOutContractManagementFee || this.form.totalOutContractManagementFee == 0) &&
                    (this.form.contractFeeForA || this.form.contractFeeForA == 0)
                ) {
                    this.form.outContractManagementRate =
                        (this.form.totalOutContractManagementFee / this.form.contractFeeForA) * 100 || undefined
                }
            }
            //累计管理费收取金额
            if (key == 'totalOutContractManagementFee' || key == 'totalContractManagementFee') {
                if (
                    (this.form.totalOutContractManagementFee || this.form.totalOutContractManagementFee == 0) &&
                    (this.form.totalContractManagementFee || this.form.totalContractManagementFee == 0)
                ) {
                    this.form.accumulativeManagementFee =
                        this.form.totalOutContractManagementFee + this.form.totalContractManagementFee
                }
            }
            //累计总包管理费收取费率
            if (
                key == 'totalOutContractManagementFee' ||
                key == 'totalContractManagementFee' ||
                key == 'contractFeeForA'
            ) {
                if (
                    (this.form.accumulativeManagementFee || this.form.accumulativeManagementFee == 0) &&
                    (this.form.contractFeeForA || this.form.contractFeeForA == 0)
                ) {
                    this.form.accumulativeTotalManagementRate =
                        (this.form.accumulativeManagementFee / this.form.contractFeeForA) * 100 || undefined
                }
            }
            if (key == 'totalOutContractManagementFee') {
                this.form.overrideTotalManagementFee = this.form.totalOutContractManagementFee > 0 ? '是' : '否'
            }
        },
        validateForm() {
            let flag = false
            this.$refs.ruleForm.validate((e) => {
                if (e) {
                    flag = true
                }else {
                  this.$message.warning('请填写完所有必填项信息')
                }
            })
            return flag
        },
        /**
         * 提交表单
         */
        handleSubmit(status) {
            let that = this
            if (status == 'SUBMITTED') {
                if (!this.validateForm()) {
                    return
                }
            }

            // 提交表单
            that.loading = true
            that.loadingBtn[status] = true
            that.form.status = status
            let form = that.$clone(that.form)
            form.overrideTotalManagementFee = form.overrideTotalManagementFee == '是'
            addContract(form)
                .then(() => {
                    that.loading = false
                    that.loadingBtn[status] = false
                    that.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
                    that.$multiTab.close()
                })
                .catch(() => {
                    that.loadingBtn[status] = false
                    that.loading = false
                })
        }
    }
}
</script>

<style lang="less" scoped>
/deep/ .ant-form-item {
    .ant-form-item-label {
        height: 28px;
        width: 200px;
        line-height: 22px;
        padding: 6px 4px 0 0;
        text-align: right;
        white-space: pre-wrap;
        overflow: visible;
    }
    .ant-form-item-control-wrapper {
        width: calc(100% - 200px);
    }
}
.file_prompt {
    color: #999;
}
</style>
